pluginHome = new File("./plugins").listFiles().find { it.name.startsWith('cloud-tools-')}
if(pluginHome == null) pluginHome = "."

includeTargets << new File( "${pluginHome}/scripts/CloudToolsInit.groovy" )

target('default': "Runs JMeter") {
	jmeter()
}

target(jmeter: "The implementation task") {
    depends( initCloudToolsService)
    cloudToolsService.withCluster {clusterManager, clusterName, cluster ->
		def file = new File("target")
		file.mkdirs()
		if(System.getProperty("cloudtools.jmxTestFile") != null){
        	List<Integer> threadCounts = new ArrayList<Integer>();
			if(System.getProperty("cloudtools.threadCount") != null){
	    		String[] threadCountStrings = System.getProperty("cloudtools.threadCount").split("[,]");
	    		for (String s : threadCountStrings) {
	      			threadCounts.add(Integer.parseInt(s));
	    		}
			}else{
				threadCounts << 1
			}
	    	List<String> dataFiles = new ArrayList<String>();
			
	    	if(System.getProperty("cloudtools.dataFiles") != null) {
	      		dataFiles = Arrays.asList(System.getProperty("cloudtools.dataFiles").split("[,]"));
	    	}
			def jmxTestFile = new File(System.getProperty("cloudtools.jmxTestFile"))
	    	cluster.loadTest(jmxTestFile.getAbsolutePath().replace('\\', '/'), threadCounts, dataFiles);
			event('StatusFinal', ['JMeter Testing'])
    	}else{
			event("StatusError", ["You did not specify 'cloudtools.jmxTestFile'\nExample: grails -Dcloudtools.jmxTestFile=./test.jmx -Dcloudtools.threadCount=1,2,3 -Dcloudtools.dataFiles=./file1.txt,/tmp/file2.txt cloud-tools-jmeter"])
		}
	}
}
